package metar;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import util.Config;

/**
 * Schreibt die empfangenen Metar-Infos in ein LogFile
 * @author Michel Metzger, Java-Kurs 2013
 * @version 1.0.0
 *
 */
public class LogFile {

	/**
	 * Standardmäßig wird das Logfile erstellt
	 */
	private static boolean writeLogFileMode = new Boolean(Config.getStaticProperty("metarlog"));

	
	/**
	 * Name des Logfiles
	 */
	private static String logFileName = "metar.log";
	

	/**
	 * Logging ein- oder ausschalten
	 * 
	 * @param mode
	 */
	public void setLogFileMode(boolean mode) {
		writeLogFileMode = mode;
	}

	
	/**
	 * Namen für das Logfile vorgeben
	 * 
	 * @param name
	 */
	public void setLogFileName(String name) {
		logFileName = name;
	}

	
	/**
	 * Write metar info into logfile
	 * @param info
	 * @throws MetarLogException
	 */
	public static void writeToLogFile(String info) throws MetarLogException {
		if (writeLogFileMode && !logFileName.isEmpty()) {
			
			FileOutputStream fos;
			try {
				
				fos = new FileOutputStream(logFileName, true);
				OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
				out.write(info + "\r\n");
				out.close();
				
			} catch (IOException e) {
				if (MetarInfo.isDebugMode()) {
					e.printStackTrace();
				}
				throw new MetarLogException("Exception while writing lofgile!");
			}
		}
	}

}
